package com.swe727.spring2011.constraint1.client;

import java.util.Random;

import com.swe727.spring2011.constraint1.coordinate.Coordinate;

public class Grid {

	private int height;
	private int width;
	
	public Grid(int width, int height) {
		this.width = width;
		this.height = height;
	}
	
	public boolean isValidCoordinate(Coordinate coordinate) {
		int x = coordinate.getX();
		int y = coordinate.getY();
		
		return x > 0 && x < width && y > 0 && y < height;
	}
	
	public boolean isValidCoordinate(int x, int y) {		
		return x > 0 && x < width && y > 0 && y < height;
	}
	
	public Coordinate getNextMove(Coordinate currentLocation) {	
		
		int curX = currentLocation.getX();
		int curY = currentLocation.getY();
				
		while (true) {
			Random random = new Random(System.currentTimeMillis());
			double chance = random.nextDouble();
			
			if (chance < .2) {
				// 20% chance to stay here
				return currentLocation;
			} else if (chance < .3 && isValidCoordinate(curX - 1, curY - 1)) {
				// move southwest
				return new Coordinate(curX - 1, curY - 1);
			} else if (chance < .4 && isValidCoordinate(curX - 1, curY)) {
				// move west
				return new Coordinate(curX - 1, curY);
			} else if (chance < .5 && isValidCoordinate(curX - 1, curY + 1)) {
				// move northwest
				return new Coordinate(curX - 1, curY + 1);
			} else if (chance < .6 && isValidCoordinate(curX, curY + 1)) {
				// move north
				return new Coordinate(curX, curY + 1);
			} else if (chance < .7 && isValidCoordinate(curX + 1, curY + 1)) {
				// move northeast
				return new Coordinate(curX + 1, curY + 1);
			} else if (chance < .8 && isValidCoordinate(curX + 1, curY)) {
				// move east
				return new Coordinate(curX + 1, curY);
			} else if (chance < .9 && isValidCoordinate(curX + 1, curY - 1)) {
				// move southeast
				return new Coordinate(curX + 1, curY - 1);
			} else if (chance < 1.0 && isValidCoordinate(curX, curY - 1)) {
				// move south
				return new Coordinate(curX, curY - 1);
			}
		}
	}
	
	
}
